package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.excetion.DisplayableExcetion;
import cn.wolfcode.p2p.base.mapper.RealAuthMapper;
import cn.wolfcode.p2p.base.query.QueryObjext;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.BitStateUtil;
import cn.wolfcode.p2p.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RealAuthServiceImpl implements IRealAuthService {

    @Autowired
    private RealAuthMapper realAuthMapper;
    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public int insert(RealAuth record) {
        return realAuthMapper.insert(record);
    }

    @Override
    public int update(RealAuth record) {
        return realAuthMapper.updateByPrimaryKey(record);
    }

    @Override
    public RealAuth get(Long id) {
        return realAuthMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo queryPage(QueryObjext qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List list = realAuthMapper.selectAll(qo);
        return new PageInfo(list);
    }

    //提交认证
    @Override
    public void apply(RealAuth realAuth) {
        UserInfo userInfo = userInfoService.getCurrent();
        //取出实名认证码进行判断,如果不为null就提示
        if (userInfo.getRealAuthId() == null) {
            RealAuth re = new RealAuth();
            re.setAddress(realAuth.getAddress());
            re.setApplier(UserContext.getCount());//设置审核人
            re.setApplyTime(new Date());
            re.setBornDate(realAuth.getBornDate());
            re.setIdNumber(realAuth.getIdNumber());
            re.setImage1(realAuth.getImage1());
            re.setImage2(realAuth.getImage2());
            re.setRealName(realAuth.getRealName());
            re.setSex(realAuth.getSex());
            re.setState(RealAuth.STATE_NORMAL);
            this.insert(re);
            userInfo.setRealAuthId(re.getId());
            userInfoService.updateByPrimaryKey(userInfo);
        } else {
            throw new DisplayableExcetion("您已经实名认证,请不要重复认证");
        }
    }

    //审核业务
    @Override
    public void edit(Long id, Integer state, String remark) {
        //根据id获取审核对象
        RealAuth realAuth = this.get(id);
        //对象存在,且审核对象状态处于待审核状态
        if (realAuth != null && realAuth.getState() == RealAuth.STATE_NORMAL) {
            //设置相关的属性  审核人审核时间
            realAuth.setAuditor(UserContext.getCount());//设置审核人
            realAuth.setApplyTime(new Date());//审核时间
            realAuth.setRemark(remark);
            //获取当前用户的个人信息
            UserInfo userInfo = userInfoService.get(realAuth.getApplier().getId());
            //转台值从页面传过来,我们那状态值直接比对,如果审核通过
            if (state == RealAuth.STATE_PASS) {
                realAuth.setState(RealAuth.STATE_PASS);
                //  添加实名认证状态码
                userInfo.addState(BitStateUtil.HAS_REAL_AUTH);
                //  给申请人的userinfo设置IDnumber和realName
                userInfo.setRealName(realAuth.getRealName());
                userInfo.setIdNumber(realAuth.getIdNumber());
            } else {
                //如果审核拒绝
                realAuth.setState(RealAuth.STATE_REJECT);
                //  将申请人的realAuthId设置成null
                userInfo.setRealAuthId(null);
            }
            userInfoService.updateByPrimaryKey(userInfo);
            this.update(realAuth);
        }
    }
}
